package singleThreaded;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import net.htmlparser.jericho.Source;

/**
 * Page Retriever class. This class take the link and retrieve the page source
 * code.
 * @author Son Pham
 * @version 1.0
 */
public class PageRetriever
{
  /**
   * The URL of this object.
   */
  private final URL my_url;
  
  /**
   * Construct the PageRetriever object.
   * <p>Note: the_url is guaranteed valid.
   * @param the_link The link.
   * @throws MalformedURLException Malformed URL Exception.
   */
  public PageRetriever(final String the_link) throws MalformedURLException
  {
    my_url = new URL(the_link);
  }
  
  /**
   * @return The page's source (in plain HTML).
   */
  public Source retrievePageSource()
  {
    try
    {
      return new Source(my_url);
    }
    catch (final IOException e)
    {
      // Return empty source.
      return new Source("");
    }
  }
  
  /**
   * 
   * @return The URL of this object.
   */
  public URL getUrl()
  {
    return my_url;
  }
  
  /**
   * 
   * @return The protocol of this page.
   */
  public String getProtocol()
  {
    return my_url.getProtocol();
  }
  
  /**
   * 
   * @return The host of this page.
   */
  public String getHost()
  {
    return my_url.getHost();
  }
}
